package com.cn.demo4.service.impl;

import com.cn.demo4.dao.LayoutDao;
import com.cn.demo4.dao.impl.LayoutDaoImpl;
import com.cn.demo4.model.LayoutModel;
import com.cn.demo4.service.ILayoutService;
import com.cn.demo4.util.Tool;
import redis.clients.jedis.Jedis;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Administrator
 */
public class LayoutServiceImpl implements ILayoutService {
    @Override
    public List<LayoutModel> findAll(LayoutModel layoutModel) {
        LayoutDao layoutDao = new LayoutDaoImpl();
        return layoutDao.findAll(layoutModel);
    }

    @Override
    public String add(LayoutModel layoutModel) {
        LayoutDao layoutDao = new LayoutDaoImpl();
        int result = layoutDao.add(layoutModel);
        if(result == 1){
            Jedis jedis = new Jedis( );
            jedis.del("layoutImg");
            jedis.del("layoutId");
            jedis.del("layoutStatus");
            jedis.del("layoutContent");
            jedis.del("layoutSecondContent");
            jedis.del("layoutTitle");
            jedis.del("layoutSecondTitle");
            jedis.del("layoutThirdTitle");
        }
        return result == 1 ? "success" : "error";
    }

    @Override
    public LayoutModel findById(Integer id) {
        LayoutDao layoutDao = new LayoutDaoImpl();
        return layoutDao.findById(id);
    }

    @Override
    public String update(LayoutModel layoutModel) {
        LayoutDao layoutDao = new LayoutDaoImpl();
        int result = layoutDao.update(layoutModel);
        if(result ==1){
            Jedis jedis = new Jedis( );
            jedis.del("layoutImg");
            jedis.del("layoutId");
            jedis.del("layoutStatus");
            jedis.del("layoutContent");
            jedis.del("layoutSecondContent");
            jedis.del("layoutTitle");
            jedis.del("layoutSecondTitle");
            jedis.del("layoutThirdTitle");
        }
        return result == 1 ? "success" : "error";
    }

    @Override
    public int del(Integer id) {
        LayoutDao layoutDao = new LayoutDaoImpl();
        int result = layoutDao.del(id);
        if(result == 1){
            Jedis jedis = new Jedis( );
            jedis.del("layoutImg");
            jedis.del("layoutId");
            jedis.del("layoutStatus");
            jedis.del("layoutContent");
            jedis.del("layoutSecondContent");
            jedis.del("layoutTitle");
            jedis.del("layoutSecondTitle");
            jedis.del("layoutThirdTitle");
        }
        return result;
    }

    @Override
    public List<LayoutModel> getLayoutModelListForLayout() {
        List<LayoutModel> list = new ArrayList<>();
        Jedis jedis = new Jedis();
        LayoutDao layoutDao = new LayoutDaoImpl();
        Boolean exists = jedis.exists("layoutId");
        if(exists){
            //如果id存在则从数据库中拿取数据
            List<String> layoutIdList = jedis.lrange("layoutId",0,-1);
            for(int i = 0 ; i < layoutIdList.size(); i++){
                LayoutModel layoutModel = new LayoutModel();
                layoutModel.setId( Tool.stringToInt(jedis.lindex("layoutId",i)));
                layoutModel.setStatus( Tool.stringToInt(jedis.lindex("layoutStatus",i)));
                layoutModel.setContent(jedis.lindex("layoutContent",i));
                layoutModel.setSecondContent(jedis.lindex("layoutSecondContent",i));
                layoutModel.setTitle(jedis.lindex("layoutTitle",i));
                layoutModel.setSecondTitle(jedis.lindex("layoutSecondTitle",i));
                layoutModel.setThirdTitle(jedis.lindex("layoutThirdTitle",i));
                layoutModel.setImg(jedis.lindex("layoutImg",i));
                list.add(layoutModel);
            }
        }else{
            //将数据存入缓存数据库中
            list = layoutDao.getLayoutModelListForLayout();
            for(int i = 0 ; i < list.size(); i++){
                jedis.rpush("layoutId",Integer.toString(list.get(i).getId()));
                jedis.rpush("layoutStatus",Integer.toString(list.get(i).getStatus()));
                jedis.rpush("layoutContent",list.get(i).getContent());
                if(list.get(i).getSecondContent() != null){
                    jedis.rpush("layoutSecondContent",list.get(i).getSecondContent());
                }
                if (list.get(i).getTitle() != null) {
                    jedis.rpush("layoutTitle",list.get(i).getTitle());
                }
                if(list.get(i).getSecondTitle() != null){
                    jedis.rpush("layoutSecondTitle",list.get(i).getSecondTitle());
                }
                if(list.get(i).getThirdTitle() != null){
                    jedis.rpush("layoutThirdTitle",list.get(i).getThirdTitle());
                }
                if(list.get(i).getImg() != null){
                    jedis.rpush("layoutImg",list.get(i).getImg());
                }
            }
        }
        return list;
    }

    @Override
    public int getCount(LayoutModel layoutModel) {
        LayoutDao layoutDao = new LayoutDaoImpl();
        return layoutDao.getCount(layoutModel);
    }
}
